package personajes_movimiento;

import graph.dijkstra.PathGraph;

import java.awt.Graphics;
import java.awt.Point;

import objects.WorldObject;
import universe.*;



public class Jugador {

	private WorldObject imageObject;
	private int xTogo;
	private int yTogo;
	
	private int xTogoDijkstra;
	private int yTogoDijkstra;
	
	private double xDistance;
	private double yDistance;
	
	private double speed;
	private double angle;
	private PathGraph dijkstra;
	private int visionRange;
	
	private boolean calculateDijkstra=false;
	private boolean searchScreen =false;
	private int x;
	private int y;
	private int xScreen;
	private int yScreen;
	private int xClick;
	private int yClick;
	
	public Jugador (WorldObject imageObject){
		this.imageObject=imageObject;
		xTogo=(int)imageObject.getPosition().getX();
		yTogo=(int)imageObject.getPosition().getY();
		yDistance=0;
		xDistance=0;
		speed=0.1;
		dijkstra = new PathGraph();
		visionRange=1000;////range in pixelss

	}
	public Jugador (){
		imageObject=new WorldObject();
		xTogo=(int)imageObject.getPosition().getX();
		yTogo=(int)imageObject.getPosition().getY();
		yDistance=0;
		xDistance=0;
		speed=0.1;
		dijkstra = new PathGraph();
		visionRange=1000;////range in pixels
		x=0;
		y=0;
	}
	public void paint(Graphics g) {
		imageObject.paint(g);
		
	}
	
	
	
	public void updateWithDijkstra(double time, Universe world){
	/*	
		int xActual= imageObject.getXMidScreen();
		int yActual = imageObject.getYMidScreen();
	
		if(calculateDijkstra){
			dijkstra=new PathGraph();
		
			 dijkstra.generateGraph(xActual-visionRange, yActual-visionRange, xActual+visionRange, yActual+visionRange, 
					xActual, yActual, world, 
						xTogoDijkstra, yTogoDijkstra);
		
			calculateDijkstra=false;
			
		}
			
		
		if(xTogo!=xActual || yTogo!=yActual){
			
			
			double distance;
			distance = (speed*time);
			xDistance=xDistance+ Math.cos(angle)*distance;
			yDistance=yDistance+ Math.sin(angle)*distance;
			
			//para las x
			if(xTogo>xActual){
				while(xDistance>=1 ){
					xActual++;
					xDistance-=1;
					imageObject.setXMid(xActual);
					if(xActual>xTogo){
						xActual=xTogo;
						
						imageObject.setXMid(xActual);
						break;
					}
				}
			}else{
				if(xTogo<xActual){
					while(xDistance>=1 ){
						xActual--;
						xDistance-=1;
						imageObject.setXMid(xActual);
						if(xActual<xTogo ){
							xActual=xTogo;
							imageObject.setXMid(xActual);
							break;
						}
					}
				}
			}
			//para las y
			if(yTogo>yActual){
				while(yDistance>=1 ){
					yActual++;
					yDistance-=1;
					imageObject.setYMid(yActual);
					if(yActual>yTogo ){
						yActual=yTogo;
						imageObject.setYMid(yActual);
						break;
					}
				}
			}else{
				if(yTogo<yActual){
					while(yDistance>=1){
						yActual--;
						yDistance-=1;
						imageObject.setYMid(yActual);
						if(yActual<yTogo ){
							yActual=yTogo;
							imageObject.setYMid(yActual);
							break;
						}
					}
				}
			}
		}
		else{
			//System.out.println("ya alcance algo");
			
			Point point = dijkstra.getPointToMove();
			if(point!=null){
				
				xTogo=(int) point.getX();//+Constants.BOX_WIDTH/2;//-Constants.BOX_WIDTH/2;
				yTogo=(int) point.getY();//+Constants.BOX_HEIGHT/2;//-Constants.BOX_HEIGHT/2;
				
				setDesiredPosition(xTogo, yTogo);
			}
			
		}
		
	*/	
	}
	
	
	
	public void updateWithGraphicsCollitions(double time, Universe world){
	/*	
		
		int xActual= imageObject.getXMidScreen();
		int yActual = imageObject.getYMidScreen();
		
		
		
		if(xTogo!=xActual || yTogo!=yActual){
			
			double distance;
			distance = (speed*time);
			xDistance=xDistance+ Math.cos(angle)*distance;
			yDistance=yDistance+ Math.sin(angle)*distance;
			
			
			
			//para las x
			if(xTogo>xActual){
				while(xDistance>=1 && !world.collition(imageObject)){
					xActual++;
					xDistance-=1;
					imageObject.setX(xActual);
					if(xActual>xTogo && !world.collition(imageObject)){
						xActual=xTogo;
						imageObject.setX(xActual);
						break;
					}
				}
				if(world.collition(imageObject)){
					xActual--;
					imageObject.setX(xActual);
					xTogo=xActual;
					setDesiredPosition(xActual, yTogo);
					
				}
			}else{
				if(xTogo<xActual){
					while(xDistance>=1 && !world.collition(imageObject)){
						xActual--;
						xDistance-=1;
						imageObject.setX(xActual);
						if(xActual<xTogo && !world.collition(imageObject)){
							xActual=xTogo;
							imageObject.setX(xActual);
							break;
						}
					}
					if(world.collition(imageObject)){
						xActual++;
						imageObject.setX(xActual);
						xTogo=xActual;
						setDesiredPosition(xActual, yTogo);
					}
				}
			}
			//para las y
			if(yTogo>yActual){
				while(yDistance>=1 && !world.collition(imageObject)){
					yActual++;
					yDistance-=1;
					imageObject.setY(yActual);
					if(yActual>yTogo && !world.collition(imageObject)){
						yActual=yTogo;
						imageObject.setY(yActual);
						break;
					}
				}
				if(world.collition(imageObject)){
					yActual--;
					imageObject.setY(yActual);
					yTogo=yActual;
					setDesiredPosition(xTogo, yActual);
				}
			}else{
				if(yTogo<yActual && !world.collition(imageObject)){
					while(yDistance>=1){
						yActual--;
						yDistance-=1;
						imageObject.setY(yActual);
						if(yActual<yTogo && !world.collition(imageObject)){
							yActual=yTogo;
							imageObject.setY(yActual);
							break;
						}
					}
				}
				if(world.collition(imageObject)){
					yActual++;
					imageObject.setY(yActual);
					yTogo=yActual;
					setDesiredPosition(xTogo, yActual);
				}
			}
		}
		
	*/	
	}
	public void calculeAngle(){
		int y = (int)(yTogo-imageObject.getPosition().getY());
		int x = (int)(xTogo-imageObject.getPosition().getX());
		if(x<0)
			x=x*-1;
		if(y<0)
			y=y*-1;
		angle = Math.atan2(y,x);
	}
	
	public void setPositionToMove(int x,int y){
		xTogoDijkstra=x;
		yTogoDijkstra=y;
		calculateDijkstra=true;
		
	}
	
	public void setDesiredPosition(int x, int y) {
		xTogo=x;
		yTogo=y;
		calculeAngle();
		xDistance=0;
		yDistance=0;	
		
	}
	public void calculatePositionTogo(int x, int y) {
		xClick=x;
		yClick=y;
		
		xScreen = x;//-imageObject.getWidth()/2;
		yScreen = y;//-imageObject.getHeight()/2;
		 
				
		setPositionToMove(x, y);
		
	
	}
	public WorldObject getWorldObject (){
		return imageObject;
	}
	public int getX() {
		
		return (int)imageObject.getPosition().getX();
	}
	public int getY() {
		
		return (int)imageObject.getPosition().getY();
	}
	
}
